package com.vdf.sisconsolid.report;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

import com.vdf.sisconsolid.util.DatabaseResource;

public class ReportGenerator {

	private static Logger log = Logger.getLogger(ReportGenerator.class);
	
	public static void main(String[] args) {
		generateReport();
	}

	public static void generateReport() {

		try {

			JasperReport jasperReport = JasperCompileManager
					.compileReport("src\\main\\resources\\reports\\jasper\\"
							+ "Consolidation" + ".jrxml");
			// Second, create a map of parameters to pass to the report.
			Map parameters = new HashMap();
			// parameters.put("employeeChart", createEmployeeChartImage());

			// Third, get a database connection
			Connection conn = DatabaseResource.getConnection();

			JasperPrint print = JasperFillManager.fillReport(jasperReport,
					parameters, conn);
			JasperExportManager.exportReportToPdfFile(print,
					"src\\main\\resources\\reports\\" + "TestResport" + ".pdf");

			JasperPrint print2 = JasperFillManager.fillReport(jasperReport,
					new HashMap(), conn);
			ByteArrayOutputStream outputByteArray = new ByteArrayOutputStream();
			OutputStream outputfile;
			outputfile = new FileOutputStream(new File(
					"src\\main\\resources\\reports\\" + "TestResport" + ".xls"));

			// coding For Excel:
			JRXlsExporter exporterXLS = new JRXlsExporter();
			exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT,
					print2);
			exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
					outputByteArray);
			exporterXLS
					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
							Boolean.FALSE);
			exporterXLS.setParameter(
					JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
			exporterXLS.setParameter(
					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					Boolean.FALSE);
			exporterXLS.setParameter(
					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			exporterXLS.exportReport();
			outputfile.write(outputByteArray.toByteArray());
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void generateReportPDF(String jrxmlReportPath,
			String jrxmlReport, String pdfReportPath, String pdfReport,
			Map<Object, Object> parameters) {
		
		log.debug("generateReportPDF");

		try {
			String jasperReportStr = jrxmlReportPath + jrxmlReport + ".jrxml";
			log.debug("jasperReport = " + jasperReportStr);			
			JasperReport jasperReport = JasperCompileManager
					.compileReport(jasperReportStr);

			Connection conn = DatabaseResource.getConnection();

			JasperPrint print = JasperFillManager.fillReport(jasperReport,
					parameters, conn);
			JasperExportManager.exportReportToPdfFile(print, pdfReportPath
					+ pdfReport + ".pdf");
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}